package zb.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zb.blog.base.bean.Result;
import zb.blog.base.global.GlobalVar;
import zb.blog.base.sessionScope.SessionScope;
import zb.blog.bean.FriendLinkBean;
import zb.blog.dao.FriendLinkDao;
import zb.blog.service.IFriendLinkService;

import java.util.List;
import java.util.Map;

@Service
public class FriendLinkService implements IFriendLinkService {
    @Autowired
    private FriendLinkDao friendLinkDao;

    @Override
    public PageInfo<FriendLinkBean> getFriendLink() {
        PageHelper.startPage(GlobalVar.PAGE_NUM, 99999);
        List<FriendLinkBean> list = friendLinkDao.getFriendLink(1);
        PageInfo<FriendLinkBean> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<FriendLinkBean> getFriendLinkAll(Map<String, String> param) {
        Integer pageNum = Integer.valueOf(param.get("pageNum"));
        Integer pageSize = Integer.valueOf(param.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);
        List<FriendLinkBean> list = friendLinkDao.getFriendLinkAll(param);
        PageInfo<FriendLinkBean> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Result updateStatusById(Integer id, Integer status) {
        Result res = new Result();
        if (status == 0 || status == 1) {//更新状态
            friendLinkDao.updateStatusNoticeById(id, status);
            res.setMsg("更新状态成功！");
        } else if (status == -1) {//直接删除
            friendLinkDao.deleteNoticeById(id);
            res.setMsg("删除友链成功！");
        }
        res.setSuccess(GlobalVar.SUCCESS);
        return res;
    }

    @Override
    public FriendLinkBean getFriendLinkbyId(Integer id) {
        return friendLinkDao.getFriendLinkbyId(id);
    }

    @Override
    public Result editFriendLink(FriendLinkBean friendLinkBean, String type) {
        Result res = new Result();
        Integer userId = SessionScope.getLoginAuth().getUser().getId();
        if (type != null && type.equals("edit") && friendLinkBean.getId() != null) {
            friendLinkDao.updateFriendLink(friendLinkBean);
            res.setSuccess(GlobalVar.SUCCESS);
            res.setMsg("更新友链成功！");
        } else if (type != null && type.equals("add")) {
            friendLinkDao.addFriendLink(friendLinkBean);
            res.setSuccess(GlobalVar.SUCCESS);
            res.setMsg("添加友链成功！");
        } else {
            res.setSuccess(GlobalVar.FAIL);
            res.setMsg("id不存在或无法是别的操作!");
        }
        return res;
    }
}
